<?php
/**
 * Created by PhpStorm.
 * Script Name: Tenant.php
 * Create: 2023/2/16 14:37
 * Description:
 * Author: fudaoji<fdj@kuryun.cn>
 */

namespace app\admin\controller;

use app\common\model\bgf\Goods as GoodsM;
use app\common\service\XmlMini;
use app\constants\Common;

class Bgfgoods extends Base
{
    /**
     * @var GoodsM
     */
    protected $model;

    public function initialize(){
        parent::initialize();
        $this->model = new GoodsM();
    }

    /**
     * 客户管理（自主注册）
     * @return mixed
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\DbException
     * @throws \think\db\exception\ModelNotFoundException
     * Author: fudaoji<fdj@kuryun.cn>
     */
    public function index(){
        if(request()->isPost()){
            $post_data = input('post.');
            $where = [];
            !empty($post_data['search_key']) && $where['title'] = ['like', '%'.$post_data['search_key'].'%'];
            if(isset($post_data['status']) && $post_data['status'] != -1){
                $where['status'] = $post_data['status'];
            }

            $total = $this->model->total($where, true);
            if ($total) {
                $list = $this->model->getList([$post_data['page'], $post_data['limit']], $where, ['id' => 'desc'], true, 1);
                foreach ($list as $k => $v){
                    $xml = new XmlMini($v['xml']);
                    $app_info = $xml->getWeAppInfo();
                    $v['pagepath'] = $app_info['pagepath'];
                    $v['share_title'] = $xml->getTitle();
                    $list[$k] = $v;
                }
            } else {
                $list = [];
            }

            $this->success('success', '', ['total' => $total, 'list' => $list]);
        }

        $builder = new ListBuilder();
        $builder->setSearch([
            ['type' => 'text', 'name' => 'search_key', 'title' => '搜索词','placeholder' => '名称'],
            ['type' => 'select', 'name' => 'status', 'title' => '状态', 'options' => [-1 => '全部'] + Common::status()]
        ])
            ->addTopButton('addnew')
            ->addTableColumn(['title' => '序号', 'type' => 'index',  'minWidth' => 70])
            ->addTableColumn(['title' => '模版名称', 'field' => 'title',  'minWidth' => 100])
            /*->addTableColumn(['title' => '卡片标题', 'field' => 'share_title'])*/
            ->addTableColumn(['title' => '跳转路径', 'field' => 'pagepath',  'minWidth' => 500])
            //->addTableColumn(['title' => 'superid', 'field' => 'super_id',  'minWidth' => 100])
            ->addTableColumn(['title' => '状态', 'field' => 'status', 'type' => 'switch', 'options' => Common::status()])
            ->addTableColumn(['title' => '操作', 'width' => 220, 'type' => 'toolbar'])
            ->addRightButton('edit')
            ->addRightButton('delete');
        return $builder->show();
    }

    /**
     * 添加
     */
    public function add(){
        //使用FormBuilder快速建立表单页面。
        $builder = new FormBuilder();
        $builder->setMetaTitle('新增')  //设置页面标题
            ->setPostUrl(url('savepost')) //设置表单提交地址
            ->addFormItem('title', 'text', '模版名称', '长度1-50之间', [], ' minlength="1" maxlength="50"')
            ->addFormItem('xml', 'textarea', '卡片xml', '卡片xml', [], 'required style=height:400px')
            ->addFormItem('status', 'radio', '状态', '状态', Common::status())
            ->setFormData(['status' => 1]);

        return $builder->show();
    }

    /**
     * 编辑
     */
    public function edit(){
        $id = input('id');
        $data = $this->model->getOne($id);
        if(! $data){
            $this->error('id参数错误');
        }

        //使用FormBuilder快速建立表单页面。
        $builder = new FormBuilder();
        $builder->setMetaTitle('新增')  //设置页面标题
            ->setPostUrl(url('savepost')) //设置表单提交地址
            ->addFormItem('id', 'hidden', 'id', 'id')
            ->addFormItem('title', 'text', '模版名称', '长度1-50之间', [], ' minlength="1" maxlength="50"')
            ->addFormItem('xml', 'textarea', '卡片xml', '卡片xml', [], 'required style=height:400px')
            ->addFormItem('status', 'radio', '状态', '状态', Common::status())
            ->setFormData($data);

        return $builder->show();
    }

    public function savePost($jump_to = '/undefined', $data = [])
    {
        $data = input('post.');
        $xml = explode('<?xml', $data['xml']);
        if(count($xml) > 1){
            $data['xml'] = str_replace($xml[0], '', $data['xml']);
        }
        parent::savePost($jump_to, $data); // TODO: Change the autogenerated stub
    }
}